package org.example.task.controller;


import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.example.task.converter.NotificationConverterMapper;
import org.example.task.enums.NotificationStatus;
import org.example.task.model.dto.PageDTO;
import org.example.task.model.dto.notification.SendNotification;
import org.example.task.model.dto.notification.UpdateNotification;
import org.example.task.model.po.Notification;
import org.example.task.model.query.NotificationPageQuery;
import org.example.task.model.vo.R;
import org.example.task.model.vo.notification.NotificationVO;
import org.example.task.service.INotificationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 通知模块
 *
 * @module 通知模块
 */
@RestController
@RequestMapping("/notification")
@RequiredArgsConstructor
public class NotificationController {
    private final INotificationService notificationService;
    private final NotificationConverterMapper notificationConverterMapper;

    /**
     * 发送通知
     *
     * @param sendNotification 通知内容
     * @return 结果
     */
    @PreAuthorize("hasAuthority('notification_send')")
    @PostMapping
    public R send(@Valid @RequestBody SendNotification sendNotification) {
        boolean flag = notificationService.send(sendNotification);
        if (flag) {
            return R.success();
        }
        return R.error("发送失败");
    }

    /**
     * 获取通知列表
     *
     * @param notificationPageQuery 请求数据
     * @return 通知列表
     */
    @PreAuthorize("hasAuthority('notification_list')")
    @GetMapping
    public R<PageDTO<NotificationVO>> getNotificationList(@Valid @RequestBody NotificationPageQuery notificationPageQuery) {
        PageDTO<NotificationVO> pageDTO = notificationService.getList(notificationPageQuery);
        return R.success(pageDTO);
    }

    /**
     * 更新通知状态
     *
     * @param updateNotification 请求数据
     * @return 结果
     */
    @PutMapping
    public R motifyNotification(@Valid @RequestBody UpdateNotification updateNotification) {
        Notification notification = notificationConverterMapper.updateNotificationTONotification(updateNotification);
        notification.setStatus(NotificationStatus.READ);
        boolean flag = notificationService.updateById(notification);
        if (flag) {
            return R.success();
        }
        return R.error("更新失败");
    }
}
